package com.steveproject.parttimeworktree.ui.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.view.View;

import com.alibaba.fastjson.JSONArray;
import com.steveproject.parttimeworktree.App;
import com.steveproject.parttimeworktree.R;
import com.steveproject.parttimeworktree.adapter.FenleiLeftAdapter;
import com.steveproject.parttimeworktree.base.BaseActivity;
import com.steveproject.parttimeworktree.entity.PartAddressEntity;
import com.steveproject.parttimeworktree.entity.PartBaseEntity;
import com.steveproject.parttimeworktree.net.MyCallback;
import com.steveproject.parttimeworktree.net.NoValidationTask;
import com.steveproject.parttimeworktree.net.Task;
import com.steveproject.parttimeworktree.utils.SPUtils;
import com.steveproject.parttimeworktree.utils.TitleBuilder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Response;

public class Part2AddressActivity extends BaseActivity {
    @BindView(R.id.recyclerView_1)
    RecyclerView recyclerView_1;
    @BindView(R.id.recyclerView_2)
    RecyclerView recyclerView_2;
    @BindView(R.id.recyclerView_3)
    RecyclerView recyclerView_3;

    private FenleiLeftAdapter partAddressAdapter1 = null;
    private FenleiLeftAdapter partAddressAdapter2 = null;
    private FenleiLeftAdapter partAddressAdapter3 = null;

    private List<PartAddressEntity> list1 = new ArrayList<>();//省

    private List<PartAddressEntity> list2 = new ArrayList<>();//市
    private List<PartAddressEntity> list3 = new ArrayList<>();//县

    private String province = "";
    private String Region_1 = "";
    private String Region_2 = "";

    @Override
    public void setListen() {

    }

    @Override
    public void setTitleBarColor() {

    }

    @Override
    public int intiLayout() {
        return R.layout.activity_part_location;
    }

    @Override
    protected boolean isStatusBarBlack() {
        return true;
    }

    @Override
    public void initView() {

        // 初始化recycleView

        LinearLayoutManager manager = new LinearLayoutManager(this);

        recyclerView_1.setHasFixedSize(true);
        recyclerView_1.setNestedScrollingEnabled(false);
        // 第一种，直接取消动画
        RecyclerView.ItemAnimator animator = recyclerView_1.getItemAnimator();
        if (animator instanceof SimpleItemAnimator) {
            ((SimpleItemAnimator) animator).setSupportsChangeAnimations(false);
        }
        recyclerView_1.setLayoutManager(manager);


        LinearLayoutManager manager1 = new LinearLayoutManager(this);

        recyclerView_2.setHasFixedSize(true);
        recyclerView_2.setNestedScrollingEnabled(false);
        // 第一种，直接取消动画
        RecyclerView.ItemAnimator animator1 = recyclerView_2.getItemAnimator();
        if (animator1 instanceof SimpleItemAnimator) {
            ((SimpleItemAnimator) animator1).setSupportsChangeAnimations(false);
        }
        recyclerView_2.setLayoutManager(manager1);


        LinearLayoutManager manager2 = new LinearLayoutManager(this);

        recyclerView_3.setHasFixedSize(true);
        recyclerView_3.setNestedScrollingEnabled(false);
        // 第一种，直接取消动画
        RecyclerView.ItemAnimator animator2 = recyclerView_3.getItemAnimator();
        if (animator2 instanceof SimpleItemAnimator) {
            ((SimpleItemAnimator) animator2).setSupportsChangeAnimations(false);
        }
        recyclerView_3.setLayoutManager(manager2);


        new TitleBuilder(this).setTitleText("现居住城市")
                .setLeftIcoListening(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });
    }

    @Override
    public void initData() {
        NoValidationTask.getApiService().getProvince2().enqueue(new MyCallback<PartBaseEntity>(Part2AddressActivity.this) {
            @Override
            protected void onSuccess(Response<PartBaseEntity> response) {
                try {
                    assert response.body() != null;
                    String jsonStr = (String) response.body().getData();
                    JSONArray jsonArray = JSONArray.parseArray(jsonStr);
                    if (jsonArray.size() > 0) {
                        for (int i = 0; i < jsonArray.size(); i++) {
                            PartAddressEntity partAddressEntity = new PartAddressEntity();
                            partAddressEntity.setName(jsonArray.getJSONObject(i).getString("name"));
                            partAddressEntity.setmId(jsonArray.getJSONObject(i).getString("id"));
                            list1.add(partAddressEntity);
                        }
                        if (partAddressAdapter1 == null) {
                            partAddressAdapter1 = new FenleiLeftAdapter(Part2AddressActivity.this, list1, "0");
                            recyclerView_1.setAdapter(partAddressAdapter1);
                            partAddressAdapter1.setOnItemClickListener(new FenleiLeftAdapter.OnItemClickListener() {
                                @Override
                                public void onItemClick(View view, int position) {
                                    province = list1.get(position).getName();
                                    getPartRegion(list1.get(position).getmId());
                                    if (partAddressAdapter3 != null) {
                                        list3.clear();
                                        partAddressAdapter3.notifyDataSetChanged();
                                    }
                                }
                            });
                        }
                        partAddressAdapter1.notifyDataSetChanged();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            protected void onFailure(Throwable t) {

            }
        });
    }

    private void getPartRegion(String id) {
        NoValidationTask.getApiService().getRegion2(id).enqueue(new MyCallback<PartBaseEntity>(Part2AddressActivity.this) {
            @Override
            protected void onSuccess(Response<PartBaseEntity> response) {
                try {
                    list2.clear();
                    assert response.body() != null;
                    String jsonStr = (String) response.body().getData();
                    JSONArray jsonArray = JSONArray.parseArray(jsonStr);
                    if (jsonArray.size() > 0) {
                        for (int i = 0; i < jsonArray.size(); i++) {
                            PartAddressEntity partAddressEntity = new PartAddressEntity();
                            partAddressEntity.setName(jsonArray.getJSONObject(i).getString("name"));
                            partAddressEntity.setmId(jsonArray.getJSONObject(i).getString("id"));
                            list2.add(partAddressEntity);
                        }
                        partAddressAdapter2 = new FenleiLeftAdapter(Part2AddressActivity.this, list2, "1");
                        recyclerView_2.setAdapter(partAddressAdapter2);
                        partAddressAdapter2.setOnItemClickListener(new FenleiLeftAdapter.OnItemClickListener() {
                            @Override
                            public void onItemClick(View view, int position) {
                                Region_1 = list2.get(position).getName();
                                getPartRegion2(list2.get(position).getmId());
                            }
                        });
                        partAddressAdapter2.notifyDataSetChanged();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            protected void onFailure(Throwable t) {

            }
        });

    }

    private void getPartRegion2(String id) {
        NoValidationTask.getApiService().getRegion2(id).enqueue(new MyCallback<PartBaseEntity>(Part2AddressActivity.this) {
            @Override
            protected void onSuccess(Response<PartBaseEntity> response) {
                try {
                    list3.clear();
                    assert response.body() != null;
                    String jsonStr = (String) response.body().getData();
                    JSONArray jsonArray = JSONArray.parseArray(jsonStr);
                    if (jsonArray.size() > 0) {
                        for (int i = 0; i < jsonArray.size(); i++) {
                            PartAddressEntity partAddressEntity = new PartAddressEntity();
                            partAddressEntity.setName(jsonArray.getJSONObject(i).getString("name"));
                            partAddressEntity.setmId(jsonArray.getJSONObject(i).getString("id"));
                            list3.add(partAddressEntity);
                        }
                        partAddressAdapter3 = new FenleiLeftAdapter(Part2AddressActivity.this, list3, "2");
                        recyclerView_3.setAdapter(partAddressAdapter3);
                        partAddressAdapter3.setOnItemClickListener(new FenleiLeftAdapter.OnItemClickListener() {
                            @Override
                            public void onItemClick(View view, int position) {
                                Region_2 = list3.get(position).getName();
                                SPUtils.put(App.getContext(), "region", Region_1);
                                finish();
                            }
                        });
                        partAddressAdapter3.notifyDataSetChanged();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            protected void onFailure(Throwable t) {

            }
        });

    }
}
